//index.js
//获取应用实例
var app = getApp()
var queryAddressUrl = "addressApi/queryAddress";
var defaultAddressUrl = "addressApi/updateDefaultAddress";
var deleteAddressUrl = "addressApi/deleteAddress";
var whereGo = "";
Page({
  data: {
    curIndex: 0,
    addressList: []
  },
  curSelect: function (e) {
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    var addressP = e.currentTarget.dataset.provincename;
    var addressC = e.currentTarget.dataset.cityname;
    var addressQ = e.currentTarget.dataset.countryname;
    var addressDetail = e.currentTarget.dataset.addressdetail;
    var person = e.currentTarget.dataset.person;
    var tel = e.currentTarget.dataset.tel;
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];  //上一个页面

    if (whereGo == 1) {
      prevPage.setData({
        adress: {
          addressP: addressP,
          addressC: addressC,
          addressQ: addressQ,
          addressDetail: addressDetail,
        },
        person: person,
        tel: tel,
        isUpdate: 2,
      });
      prevPage.updateType = 2;
      wx.navigateBack({
        delta: 1
      });

    }
  },

  selectTap: function (e) {
   // console.log(e)
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;

    wx.request({
      url: app.globalData.requestUrl + defaultAddressUrl,
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      data: {//这里写你要请求的参数
        id: id,
        memberId: app.globalData.userId,
        deviceType: "C142001"
      },

      success: (res) => {
        console.log(res)
        //wx.navigateBack({})
        this.setData({
          curIndex: index
        });
      }
    })

  },

  addAddess: function () {
    wx.navigateTo({
      url: "/pages/address-add/address-add"
    })
  },

  editAddess: function (e) {
    console.log(e)
    wx.navigateTo({
      url: "/pages/address-add/address-add?id=" + e.currentTarget.dataset.id + "&person=" + e.currentTarget.dataset.person + "&tel=" + e.currentTarget.dataset.tel + "&addressDetail=" + e.currentTarget.dataset.addressdetail + "&provincename=" + e.currentTarget.dataset.provincename + "&cityname=" + e.currentTarget.dataset.cityname + "&countryname=" + e.currentTarget.dataset.countryname
    })
  },




  onLoad: function (e) {

    whereGo = e.whereGo;
  },
  onShow: function () {
    this.initShippingAddress();
  },
  initShippingAddress: function () {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl + queryAddressUrl,
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      data: {//这里写你要请求的参数
        memberId: app.globalData.userId
      },
      success: (res) => {
        console.log(res)

        var index;
        for (var i = 0; i < res.data.list.length; ++i) {
          if (res.data.list[i].defaultAddress == "C129001") {
            index = i;
          }
        }

        that.setData({
          curIndex: index,
          addressList: res.data.list

        });

      }
    })
  },
  deleteAddress: function (e) {
   // console.log(e)
    var that = this;
    var id = e.currentTarget.dataset.id;
    console.log(id)
    wx.showModal({
      title: '提示',
      content: '确定要删除该收货地址吗？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.requestUrl + deleteAddressUrl,
            header: {
              'content-type': 'application/json'
            },
            method: 'POST',
            data: {
              memberId: app.globalData.userId,
              id: id,
              deviceType: "C142001"
            },
            success: (res) => {
              console.log(res)
              wx.navigateBack({})
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }


})
